﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ConsoleApp1
{
    public class OrderDetailsInsert
    {
        public string orderno { get; set; }
        public DateTime time { get; set; }
        public char orderStatus { get; set; }
        public Decimal actualPayment { get; set; }
        public int GoodsId { get; set; }
        public string Total { get; set; }
        public int Number { get; set; }
        public string Price { get; set; }
        public string Remark { get; set; }
    }

    public class OrderDetails
    {
        public string orderno { get; set; }
        public DateTime time { get; set; }
        public char orderStatus { get; set; }
        public bool freeShipping { get; set; }
        public Decimal fullCut { get; set; }
        public Decimal originalCost { get; set; }
        public Decimal actualPayment { get; set; }
        public GoodsInfoList[] goods { get; set; }
    }

    public class GoodsInfoList
    {
        public int UserId { get; set; }
        public int GoodsId { get; set; }
        public string Total { get; set; }
        public int Number { get; set; }
        public string Price { get; set; }
        public string shopname { get; set; }
        public string manmoney { get; set; }

    }

    class JsonClass
    {
        public List<OrderDetailsInsert> json2Object()
        {
            var json = "[{\"orderNo\": \"3213123123123\",\"time\": \"2016-09-09 12:23:33\",\"orderStatus\":\"1\", \"freeShipping\": true, \"fullCut\": 20,\"originalCost\": 340, \"actualPayment\": 320,\"goods\": [";
            json += " {\"UserId\": \"5\",\"GoodsId\": \"8\", \"Total\": 40, \"Number\": 2, \"Price\": \"20.00\", \"shopname\": \"两颗牙\", \"manmoney\": \"200\", \"jianmoney\": \"0\",\"jianyoufei\": \"10\"},";
            json += " {\"UserId\": \"5\",\"GoodsId\": \"7\", \"Total\": 60, \"Number\": 1, \"Price\": \"60.00\",\"shopname\": \"两颗牙\", \"manmoney\": \"200\", \"jianmoney\": \"0\",\"jianyoufei\": \"10\"},";

            json += " ]} ]";

            OrderDetails[] datas = JsonConvert.DeserializeObject<OrderDetails[]>(json);
            List<OrderDetailsInsert> insert = new List<OrderDetailsInsert>();
            foreach (OrderDetails data in datas)
            {
                var shopname = string.Empty;//判断是否同一个商家
                foreach (var item in data.goods)
                {
                    OrderDetailsInsert getinfo = new OrderDetailsInsert();
                    getinfo.orderno = data.orderno;
                    getinfo.time = data.time;
                    getinfo.orderStatus = data.orderStatus;
                    getinfo.actualPayment = data.actualPayment;
                    getinfo.orderno = data.orderno;
                    if (data.freeShipping == true)
                    {
                        getinfo.Remark = "此商品符合包邮条件及满" + item.manmoney + "减" + data.fullCut + "条件：订单总金额:" + data.originalCost + "符合满减条件减去：" + data.fullCut + "实际付款金额:" + data.actualPayment;
                    }
                    else if (!string.IsNullOrEmpty(data.fullCut.ToString()) && data.fullCut != 0)
                    {
                        getinfo.Remark = "此商品符合满" + item.manmoney + "减" + data.fullCut + "条件：订单总金额:" + data.originalCost + "符合满减条件减去：" + data.fullCut + "实际付款金额:" + data.actualPayment;
                    }
                    else
                    {
                        getinfo.Remark = "订单实际付款金额:" + data.actualPayment;
                    }
                    getinfo.GoodsId = item.GoodsId;
                    getinfo.Total = item.Total;
                    getinfo.Number = item.Number;
                    getinfo.Price = item.Price;
                    insert.Add(getinfo);
                }
            }
            return insert;
        }
    }

    
}
